#include <iostream>
#include <vector>
using namespace std;

void printf(vector<int> &v) {
    for (auto it = v.begin(); it != v.end(); it++) {
        cout << *it << " ";
    }
}

void test01() {
    vector<int> v1;
    for (int i = 0; i < 10; i++) {
        v1.push_back(i);
    }
    cout << "交换前：" << endl;
    printf(v1);
    vector<int> v2;
    for (int i = 10; i > 0; i--) {
        v2.push_back(i);
    }
    printf(v2);
    v2.swap(v1);
    cout << endl;
    cout << "交换后：" << endl;
    printf(v1);
    printf(v2);
}

void test02() {
    //swap交换收缩内存
    vector<int> v;
    for (int i = 0; i < 100000; i++) {
        v.push_back(i);
    }
    cout << "原来：" << endl;
    cout << "v的容量=" << v.capacity() << endl;
    cout << "v的大小=" << v.size() << endl;

    v.resize(22);
    cout << "重构后：" << endl;
    cout << "v的容量=" << v.capacity() << endl;
    cout << "v的大小=" << v.size() << endl;

    cout << "交换释放后:" << endl;
    vector<int>(v).swap(v); //(v)相当于匿名对象，不用会被系统回收，释放内存,内存释放了，运行就快了，现阶段暂时还用不上，先了解
    cout << "v的容量=" << v.capacity() << endl;
    cout << "v的大小=" << v.size() << endl;
}

int main() {
    //test01();
    test02();
    return 0;
}